package com.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.service.PermissionService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.vo.PermissionVo;
import com.sxt.sys.vo.NoticeVo;

@RestController
@RequestMapping("permission")
public class PermissionController {
	
	
	@Autowired
	private PermissionService permissionService;
	
	//查询
	@RequestMapping("loadAllPermission")
	public DataGridView loadAllPermission(PermissionVo permissionVo) {
		permissionVo.setType(SYSConstast.MENU_TYPE_MENU);
		return permissionService.qureyAllPermissionByMenu(permissionVo);
	}
	
	@RequestMapping("deletePermission")
	public ResultObj deletePermission(PermissionVo permissionVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = permissionService.deletePermission(permissionVo.getId());
			code = info;
			msg = info > 0 ?SYSConstast.DELETE_LOGININFOR_TRUE:SYSConstast.DELETE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.DELETE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("addPermission")
	public ResultObj addNotice(PermissionVo permissionVo) {
		Integer code=1;
		String msg="";
		try {
			permissionVo.setType(SYSConstast.MENU_TYPE_MENU);
			Integer info = permissionService.addPermission(permissionVo);
			code = info;
			msg = info > 0 ?SYSConstast.ADD_LOGININFOR_TRUE:SYSConstast.ADD_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.ADD_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("updatePermission")
	public ResultObj updateNotice(PermissionVo permissionVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = permissionService.updatePermission(permissionVo);
			code = info;
			msg = info > 0 ?SYSConstast.UPDATE_LOGININFOR_TRUE:SYSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("loadPermissionManagerLeftTreeJson")
	public List<TreeNode> loadPermissionManagerLeftTreeJson(PermissionVo permissionVo){
		List<TreeNode> node = new ArrayList<>();
		List<Permission> list = permissionService.queryAllPermissionForlist(permissionVo);
		for (Permission permission : list) {
			Boolean isParent =permission.getParent()==SYSConstast.DEPT_ISPARENT_TRUE?true:false;
			Boolean open = permission.getOpen()==SYSConstast.DEPT_OPEN_TRUE?true:false;
			node.add(new TreeNode(permission.getId(), permission.getPid(), permission.getName(), isParent, open));
		}
		return node;
		
	}
	
	
	
	//取出排序最大值
	@RequestMapping("loadMaxOrderNumber")
	public Map<String, Integer> loadMaxOrderNumber(){
		Map<String, Integer> map = new HashMap<>();
		Integer value = permissionService.queryMaxOrderNumber();
		
		map.put("value", value);
		
		return map;
	}
	
	@RequestMapping("isThisPermissionObjHasChildren")
	public Map<String,Object> isThisPermissionObjHasChildren(PermissionVo permissionVo){
		Map<String,Object> map=new HashMap<>();
		Integer value=this.permissionService.isThisPermissionObjHasChildren(permissionVo.getId());
		map.put("value", value>0?true:false);
		return map;
	}
	
	
	/**
	 * 权限管理
	 * 
	 */
	
	@RequestMapping("loadAllPermissionByTypePermission")
	public DataGridView loadAllPermissionByTypePermission(PermissionVo permissionVo) {
		permissionVo.setType(SYSConstast.MENU_TYPE_PERMISSION);
		return permissionService.queryAllPermissionByTypePermission(permissionVo);
	}
	
	@RequestMapping("addPermissionByPermission")
	public ResultObj addPermissionByPermission(PermissionVo permissionVo) {
		Integer code=1;
		String msg="";
		try {
			permissionVo.setType(SYSConstast.MENU_TYPE_PERMISSION);
			Integer info = permissionService.addPermission(permissionVo);
			code = info;
			msg = info > 0 ?SYSConstast.ADD_LOGININFOR_TRUE:SYSConstast.ADD_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.ADD_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
}
